package com.ali.asyncgroup.handler;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

public class AsyncGroupHandlerClient {
    public static void main(String[] args) {
        //构建线程池
        try (NioEventLoopGroup group = new NioEventLoopGroup()) {
            //构建客户端启动对象
            Bootstrap bootstrap = new Bootstrap();
            //设置线程池
            bootstrap.group(group);
            //设置通道
            bootstrap.channel(NioSocketChannel.class);
            //设置处理器
            bootstrap.handler(new ChannelInitializer<SocketChannel>() {
                /**
                 * 通道初始化
                 */
                @Override
                protected void initChannel(SocketChannel socketChannel) {
                    //添加处理器
                    socketChannel.pipeline().addLast(new MyAsyncGroupHandlerClientHandler());
                }
            });
            //构建客户端连接，同步阻塞，等待操作完成
            ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 8080).sync();
            //监听关闭通道，同步阻塞，等待操作完成
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
